//
//  MobilebankView.m
//  SZBank
//
//  Created by rimi on 16/7/20.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "MobilebankView.h"
@interface MobilebankView()
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSString *qrString;
}
@property (nonatomic,strong) UIImageView *bgImageView;/**  背景图 */

@property (nonatomic,strong) UIImageView *qrCodeView;/**  二维码 */
@end
@implementation MobilebankView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        [self addSubview:self.bgImageView];
        /**  手机银行地址数据请求 */
        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:@"402880c4400b2b0e01400b2e2ed20001" success:^(id sender) {
            NSDictionary *dic = [sender[2] objectForKey:@"protocolData"];
            NSString *urlString = [dic objectForKey:@"二维码地址"][0];
            qrString = [urlString substringToIndex:25];
            [self initUserInterface];
        } failure:nil];
    }
    return self;
}

- (void)initUserInterface{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    /**  恢复默认 */
    [filter setDefaults];
    /**  给过滤器添加地址 */
    NSString *dataString = qrString;
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    /**  设置滤镜数据 */
    [filter setValue:data forKeyPath:@"inputMessage"];
    /**  获取输出的二维码 */
    CIImage *myQrImage = [filter outputImage];
    self.qrCodeView.image = [self createNonInterpolatedUIImageFormCIImage:myQrImage withSize:200];
    [self addSubview:self.qrCodeView];
}
/**  高清化二维码 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    /**  创建bitmap */
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    /**  保存bitmap到图片 */
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _bgImageView.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_手机银行"];
    }
    return _bgImageView;
}

- (UIImageView *)qrCodeView{
    if (!_qrCodeView) {
        _qrCodeView = [[UIImageView alloc] initWithFrame:CGRectMake(frameWidth * 0.412, frameHeight * 0.365, 200, 200)];
        _qrCodeView.backgroundColor = [UIColor clearColor];
    }
    return _qrCodeView;
}
@end
